package com.alphaking.service.mingweicai;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alphaking.constant.CategoryConstant;
import com.alphaking.constant.SortConstant;
import com.alphaking.model.Blog;
import com.alphaking.model.Category;
import com.alphaking.pojo.common.PagerResult;
import com.alphaking.pojo.common.SearchBean;
import com.alphaking.pojo.common.SearchCondition;
import com.alphaking.service.base.ImportDaoService;
import com.alphaking.util.DataConvertorUtil;

@Service
@Transactional
public class BlogService extends ImportDaoService {

	/**
	 * 添加商品页面获取到第一级分类
	 * 
	 * @param model
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 */
	public void initCategorys(Model model) throws NoSuchFieldException, SecurityException {

		// 找到商品分类
		// 父级id
		SearchCondition searchCondition = SearchCondition.addSearchBeanByMap(null, "parentCategoryId",
				SearchBean.OPERATOR_IN, CategoryConstant.ONE_LEVEL_PARENTID + "", null);
		// 非删除
		SearchCondition.addSearchBeanByMap(searchCondition, "isDeleted", SearchBean.OPERATOR_IN,
				CategoryConstant.ISDELETED_FALSE + "", null);
		// 非隐藏
		SearchCondition.addSearchBeanByMap(searchCondition, "isHidden", SearchBean.OPERATOR_IN,
				CategoryConstant.PRODUCT_CATEGORY_IS_NOT_HIDDEN + "", null);
		// 进行查询
		List<Category> productCategorys = this.productCategoryDao.listModel(false, searchCondition).getResultSet();
		model.addAttribute("productCategorys", productCategorys);

	}

	// 获取到查询category的条件
	public SearchCondition getCategorySearchCondition(String parentId) {
		// 父级id
		SearchCondition searchCondition = SearchCondition.addSearchBeanByMap(null, "parentCategoryId",
				SearchBean.OPERATOR_IN, parentId + "", null);
		// 非删除
		SearchCondition.addSearchBeanByMap(searchCondition, "isDeleted", SearchBean.OPERATOR_IN,
				CategoryConstant.ISDELETED_FALSE + "", null);
		// 非隐藏
		SearchCondition.addSearchBeanByMap(searchCondition, "isHidden", SearchBean.OPERATOR_IN,
				CategoryConstant.PRODUCT_CATEGORY_IS_NOT_HIDDEN + "", null);
		return searchCondition;
	}

	/**
	 * 获取到二级分类
	 * 
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 */
	public List<Category> getPriorityCategory(String parentId) throws NoSuchFieldException, SecurityException {

		// 获取到查询条件
		SearchCondition searchCondition = getCategorySearchCondition(parentId);
		// 进行查询
		List<Category> productCategorys = this.productCategoryDao.listModel(false, searchCondition).getResultSet();
		return productCategorys;
	}

	public void addBlog(String mapAttr) {

		Blog blog = DataConvertorUtil.json2object(mapAttr,Blog.class);
		blog.setBlogId(UUID.randomUUID().toString());
		this.blogDao.save(blog);
//		System.out.println(1/0);

	}

	@SuppressWarnings("static-access")
	public void findBlogs(Model model, String mapArr) throws NoSuchFieldException, SecurityException {

		// 查询条件
		SearchCondition searchCondition = SearchCondition.initSearchCondition(null);
		searchCondition.setSize(2);
		Map<String, String> map = new HashMap<>();
		if (mapArr != null) {
			map = DataConvertorUtil.json2object(mapArr, Map.class, String.class, String.class);
			if(map.get("blogId") !=null && map.get("blogId") != ""){//按照id进行查询
				searchCondition.addSearchBeanByMap(searchCondition,
						"blogId", SearchBean.OPERATOR_IN,map.get("blogId"), null);
			}else{
				
				if (map.get("title") != null && map.get("title") != "") {//按照标题进行查询
					searchCondition.addSearchBeanByMap(searchCondition,
							"title", SearchBean.OPERATOR_LIKE,map.get("title"), null);
				}
				
			}
			// 如果前台按照分页查询
			if (map.get("pageNum") != null && map.get("pageNum") != "") {
				searchCondition.setPageNum(Integer.valueOf(map.get("pageNum")));
			}
		}
		// 按照时间排序
		SearchCondition.addSearchBeanByMap(searchCondition, "createTime", SearchBean.OPERATOR_SORT,
				SortConstant.JIANGXU, 0);
		
	    PagerResult<Blog> pagerResult = this.blogDao.listModel(true, searchCondition);
	    // 如果带有页数，则进行删除
		model.addAttribute("mapArr", map);
		model.addAttribute("mapJson", mapArr);
		model.addAttribute("pagerResult", pagerResult);
		model.addAttribute("blogs", pagerResult.getResultSet());

	}

	@SuppressWarnings("static-access")
	public void findBlog(String id, Model model) throws NoSuchFieldException, SecurityException {

		Blog blog = this.blogDao.getById(id);
		model.addAttribute("blog", blog);
	}

	@SuppressWarnings("static-access")
	public void updateBlog(String mapAttr) throws NoSuchFieldException, SecurityException {

		// 修改博客
		Blog blog = DataConvertorUtil.json2object(mapAttr, Blog.class);
		Blog uBlog = this.blogDao.getById(blog.getBlogId());
		uBlog.setContent(blog.getContent());
		uBlog.setTitle(blog.getTitle());
		uBlog.setOneLevelCategoryId(blog.getOneLevelCategoryId());
		uBlog.setCreateTime(blog.getCreateTime());
		uBlog.setIspublished(blog.getIspublished());
		uBlog.setPublisher(blog.getPublisher());
		this.blogDao.update(uBlog);

	}
	
	@SuppressWarnings("static-access")
	public void deleteBlog(String id) throws NoSuchFieldException, SecurityException{
		
		this.blogDao.delete(id);
	}
	
	

}
